<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body><div class="box box-info">
    <div class="box-header with-border">
        <h3 class="box-title">个人信息</h3>
    </div>
    <!-- /.box-header -->
    <!-- form start -->
    <form class="form-horizontal">
        <div class="box-body">
            <div class="form-group">
                <label for="usernameId" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="usernameId">
                </div>
            </div>
            <div class="form-group">
                <label for="emailId" class="col-sm-2 control-label">邮箱</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="emailId">
                </div>
            </div>
            <div class="form-group">
                <label for="phoneId" class="col-sm-2 control-label">手机号</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="phoneId">
                </div>
            </div>
        </div>

        <!-- /.box-body -->
        <div class="box-footer">
            <button type="button" class="btn btn-default btn-cancel">Cancel</button>
            <button type="button" class="btn btn-info pull-right btn-add">Save</button>
        </div>
        <!-- /.box-footer -->

    </form>
</div>
<script type="text/javascript" src="/bower_components/ztree/jquery.ztree.all.min.js"></script>
<script type="text/javascript" src="/bower_components/layer/layer.js"></script>
<script type="text/javascript">

    $(function () {
        $(".box-footer").on("click",".btn-add",doSaveOrUpdate)
            .on("click",".btn-cancel",doCancel);
        doInitFormData();
    })
    function doSaveOrUpdate() {
        //获取表单的数据
        var rowData=$("#mainID").data("rowData");
        var params=doGetEditFormData();
        if (rowData){
            params.id=rowData.user.id;
            $("#mainID").removeData("rowData");
        }
        //console.log(params);
        //发送请求
        var insertUrl="/user/doSaveObject";
        var updateUrl="/user/doUpdateObject";
        var url=rowData?updateUrl:insertUrl;
        $.post(url,params,function (result) {
            if(result.state==1){
                alert("操作成功");
                doCancel();
            }else {
                alert(result.message);
            }
        })
    }
    function doCancel() {
        $("#mainID").load("user/user_list");
    }
    function doGetEditFormData() {
        var params={
            username: $("#usernameId").val(),
            email: $("#emailId").val(),
            mobile: $("#phoneId").val(),
        };
        console.log(params);
        return params;
    }

    function doInitFormData() {
        var data=$("#mainID").data("rowData");
        if (!data)return;//判断是添加还是修改？ 如果是添加就跳出
        //如果修改，把密码div移除
        $(".pwdDiv").remove();
        //写入值
        $("#usernameId").val(data.user.username);
        $("#emailId").val(data.user.email);
        $("#phoneId").val(data.user.mobile);
    }


</script>

</body>
</html>